import 'package:flutter/material.dart'; //dartUI包
import 'package:flutter_easyrefresh/easy_refresh.dart';  //easyrefresh刷新API
import 'dart:convert';  //json解析API
import 'package:fluttertoast/fluttertoast.dart';  //消息弹框API
import 'package:flutter_screenutil/flutter_screenutil.dart';  //自适应布局API
import './lists_info.dart';  //引入详情页
import '../server/getdata.dart';  //引入接口调用类

/*
首页列表
*/
class FirstPages extends StatefulWidget{
  @override
  _FirstPagesState createState() => _FirstPagesState();
}

class _FirstPagesState extends State<FirstPages>{
  List<Map> listsData = [];
  int page = 1;
  var tip="加载完成";
  GlobalKey<RefreshFooterState> _footerKey =
  new GlobalKey<RefreshFooterState>();

  //页面加载时取page等于1的数据
  @override
  void initState() {
    super.initState();
    _getNewLists();
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.instance = ScreenUtil(width: 750,height: 1334)..init(context);

    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
          title: const Text("研招-首页")
      ),
      //外层包裹easyrefresh方法
      body: EasyRefresh(
        child:ListView(
          //处理越界屏幕爆出异常
          shrinkWrap: true,
          children: <Widget>[
            _newLists(),
          ],
        ),

        //底部刷新属性
        refreshFooter: ClassicsFooter(
          key:_footerKey,
          bgColor:Colors.white,
          textColor: Colors.blue,
          moreInfoColor: Colors.blue,
          showMore: true,
          noMoreText: tip,
          loadReadyText:'上拉加载....',
          moreInfo: '',
          loadingText:'',
          loadText:'',
        ),
        loadMore:()async{

          if(tip != '暂无更多数据'){
            _getNewLists();
          }else{
            Fluttertoast.showToast(
                msg: "已经到底了",
                toastLength: Toast.LENGTH_SHORT,
                gravity: ToastGravity.CENTER,
                timeInSecForIos: 1,
                backgroundColor: Colors.blue,
                textColor: Colors.white,
                fontSize: 14.0
            );
          }
        }
      ),
    );
  }

  //绘制列表组件
  Widget _newLists() {
    if(listsData.length!=0){
        List<Widget> listWidget = listsData.map((val){
          return InkWell(
            child: Container(
              alignment: Alignment.centerLeft,
              color: Colors.white,
              margin: EdgeInsets.only(bottom: 5.0),
              width:ScreenUtil().setWidth(750),
              padding: EdgeInsets.fromLTRB(ScreenUtil().setWidth(20), ScreenUtil().setWidth(20), ScreenUtil().setWidth(30), ScreenUtil().setWidth(20)),
              child: Row(
                children: <Widget>[
                    Image(
                      image: AssetImage("images/1.jpg"),
                      width:ScreenUtil().setWidth(120),
                      height:ScreenUtil().setWidth(120),
                      fit:BoxFit.fill,
                    ),
                    Container(
                      margin: EdgeInsets.fromLTRB( ScreenUtil().setWidth(20),0.0,ScreenUtil().setWidth(10),0.0,),
                      child: Text(
                        val['name'],
                        textAlign:TextAlign.left,
                        maxLines: 1,
                      ),
                    ),
                ],
              ),
            ),
            onTap: (){
              Navigator.push(context, new MaterialPageRoute(builder: (BuildContext context){
                return new InfoPages(index:val['index']);
              }));
            },
          );
        }).toList();
        //返回一个流布局
        return Wrap(
          spacing: 1,
          children: listWidget,
        );

    }else{
      return Center(
        child:Text('加载中......'),
      );
    }
  }

  //调用接口数据方法   每次调用完毕page++
  void _getNewLists(){
    //数据整理
    var formData = {'page':page,'format':'json'};

    //调用接口并处理返回数据
    request('SchoolList',formData: formData).then((val){
      var data = json.decode(val.toString());
      List<Map> newSchoolList = (data['data'] as List).cast();

      //当检测到没有数据时  赋值tip等于暂无更多数据以触发暂无更多数据样式
      if(newSchoolList.length == 0){
        tip = '暂无更多数据';
      }

      //重绘UI
      setState(() {
        listsData.addAll(newSchoolList);   //addAll
      });
    });
    page++;
//    DefaultAssetBundle.of(context).loadString('').then((value){
//      var data = json.decode(value.toString());
//      String _page = page.toString();
//      List<Map> newSchoolList = (data[_page] as List).cast();
//      setState(() {
//        page++;
//        listsData.addAll(newSchoolList);
//      });
//    });
  }
}
